home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Using char array in functions
- Date: 25 Feb 1996 16:31:16 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gqv0kINNd2k@keats.ugrad.cs.ubc.ca>
- References: <20FEB199609155775@sundog.caltech.edu> <4gqk5m$jno@aphex.direct.ca>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4gqk5m$jno@aphex.direct.ca>,
- Ed Toivanen <etoivane@direct.ca> wrote:
- >In article <20FEB199609155775@sundog.caltech.edu>,
- >taylor@sundog.caltech.edu395-3807,MC264-33CALTECHPASADENACA91125 says...
- >>
- >>I've searched the faq's and postings, but still am looking for a clear
- >>explanation on how to use arrays of char strings in functions, especially when
- >>I want to have the function modify the array. I find I can't just pass the
- >>name of the string as a pointer as I do for single strings.
- > ^^^^
- >
- >As I just learned from my midterm(the hard way, in other words!) you have to
- >pass the address of the name of the array if you expext to modify more than a
- >copy of the thing.
-
- If that is what you learned from the midterm, you will fail the question again
- if it is presented on the final---unless your instructor actually believes that
- arrays can be modified if you pass their address, in which case stick to the
- wrong answer to get the marks.
-
- Sucking up + marks = grad school.
-
- However, in comp.lang.c and everywhere else, an array is not a pointer, and
- its location cannot be modified. When you pass an array to a function, it
- collapses into a pointer to the first element. This pointer exists only as
- a parameter---there is no variable that holds this pointer which you can
- modify.
-
- This is akin to not being able to modify assembly language labels at run time.
- In assembly language, if you say:
-
- .data
- STRING: .ascii "My string\0"
-
- you cannot change the value of "STRING:", because it is an address label, not a
- variable. (A C array is *sort of* *like* a label, but the analogy breaks down
- in all kinds of ways, so don't take it too literally. For example, a C array
- can be a local variable, but in assembly you don't declare labels that
- reference your procedure stack frame, just static storage.)
-
- If you pass the address of the name of the array, as in:
-
- char arr[5] = "abcd";
-
- ...
- foo(&arr);
-
- it is basically the same pointer as if you passed foo(arr), but the type is
- different.
-
- The expression arr is the address of arr[0], same as &arr[0] and has the
- type, in cast notation, (char *).
-
- The expression &arr is the address of the whole array, in this case a five
- element array, and has the type, in cast notation (char *[5]).
-
- You cannot change arr. Yes, this is inconsistent from the way structures are
- handled. The name of a structure can be assigned to, whereas the name of an
- array cannot be assigned to. A structure _requires_ an & operator to pass by
- reference else it is passed by value, whereas an array is automatically passed
- by reference without &, and if you use the & you get a different type. The
- naive ``obvious'' rule (that is, ``& passes by reference, lack of it passes by
- value'') seems intuitive and consistent, but unfortunately does not reflect
- the reality of the C language which treats functions and arrays specially.
-
- If your instructor, in discussing the midterm results, has taught you
- otherwise, just send him or here to this newsgroup.
-
- Another array feature to watch out for is in parameter declarations. Only in
- function parmeter declarations, if you write "type x[]", the parameter will
- actually be converted to "pointer to x". Thus writing "char **argv" is the
- same as writing "char *argv[]". Both are pointers to pointers to char. The
- latter is just a more clear notation at times. Again, this special conversion
- reinforces the idea that arrays just collapse into pointers to the first
- element.
-
- See? The language is neater than you probably thought it was...
-
-
- --
-
-